<template>
	<view>
	<!-- 	<navbar name="位置信息"></navbar>
		<map style="width: 100vw; height: 80vh;" :latitude="latitude" :longitude="longitude" :scale="scale"
			:markers="markers" :show-location="true" @tap="chooseDress"></map>
		<view class="" @tap.stop="chooseDress(latitude2,longitude2)">
			现在去
		</view> -->
	</view>
</template>

<script>
	// import amap from '@/common/amap-wx.js';
	export default {
		data() {
			return {
				latitude: 39.909, // 默认定在首都
				longitude: 116.39742,
				latitude2:'', // 默认定在首都
				longitude2:'',
				scale: 12, // 默认16
				markers: [],
				markerWidth: 20,
				markerHeight: 28,

				amapPlugin: null,
				key: '4063c210307cc0a2d99190fc4b38c7c3',

			};
		},
		onLoad(e) {
			// this.amapPlugin = new amap.AMapWX({
			// 	key: this.key
			// });
			// console.log(this.amapPlugin)
			e.latitude = Number(e.latitude)
			e.longitude = Number(e.longitude)
			console.log(e)
			// this.latitude2 = e.latitude;
			// this.longitude2 = e.longitude
			this.chooseDress(e.latitude,e.longitude)
		},
		methods: {
			//选择地址
			chooseDress(latitude,longitude) {
				uni.openLocation({
					latitude: latitude,
					longitude: longitude,
					scale: 18
				})
			},

			//   初次位置授权
			getAuthorize() {
				return new Promise((resolve, reject) => {
					uni.authorize({
						scope: "scope.userLocation",
						success: () => {
							resolve(); // 允许授权
						},
						fail: () => {
							reject(); // 拒绝授权
						},
					});
				});
			},
			// 确认授权后，获取用户位置
			getLocationInfo() {
				const that = this;
				uni.getLocation({
					type: "gcj02",
					success: function(res) {
						// 暂时
						that.longitude = res.longitude; //118.787575;
						that.latitude = res.latitude; //32.05024;
						that.markers = [{
							id: "",
							title: '',
							latitude:  res.longitude,
							longitude: res.latitude,
							iconPath: "https://bsyjk-pic.bsyjk.cn/l/70F3551718894962A77F7584746B49A9.png",
							width: that.markerWidth, //宽
							height: that.markerHeight, //高
						}, ];
						// that.getList();
					},
				});
			},
			// 拒绝授权后，弹框提示是否手动打开位置授权
			openConfirm() {
				return new Promise((resolve, reject) => {
					uni.showModal({
						title: "请求授权当前位置",
						content: "我们需要获取地理位置信息,给您导航",
						success: (res) => {
							if (res.confirm) {
								uni.openSetting().then((res) => {
									if (res[1].authSetting["scope.userLocation"] === true) {
										resolve(); // 打开地图权限设置
									} else {
										reject();
									}
								});
							} else if (res.cancel) {
								reject();
							}
						},
					});
				});
			},
			// 彻底拒绝位置获取
			rejectGetLocation() {
				uni.showToast({
					title: "你拒绝了授权，无法获得周边信息",
					icon: "none",
					duration: 2000,
				});
			},
			getList() {
				console.log("获取周围美食");
			},
		},
		onReady() {
			//   wx请求获取位置权限
			this.getAuthorize()
				.then(() => {
					//   同意后获取
					this.getLocationInfo();
				})
				.catch(() => {
					//   不同意给出弹框，再次确认
					this.openConfirm()
						.then(() => {
							this.getLocationInfo();
						})
						.catch(() => {
							this.rejectGetLocation();
						});
				});
		},
	};
</script>
